package top.deepdesigner.controller.market;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsAdmin;
import top.deepdesigner.pojo.DtsBrand;
import top.deepdesigner.pojo.DtsCategory;
import top.deepdesigner.service.DtsAdminService;
import top.deepdesigner.service.DtsBrandService;
import top.deepdesigner.service.DtsCategoryService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CatAndAdminVO;
import top.deepdesigner.vo.CatVo;
import top.deepdesigner.vo.CommonListVO;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 地区管理 <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/21 星期三 22:35:33 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/brand")
public class BrandController {

  @Autowired
  private DtsBrandService brandService;

  @Autowired
  private DtsCategoryService categoryService;

  @Autowired
  private DtsAdminService adminService;


  /**
   * 品牌制造商列表
   *
   * @param params 参数个数
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:brand:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");
    String id = (String) params.get("id");
    String name = (String) params.get("name");

    CommonListVO<DtsBrand> brandListVO = new CommonListVO<>();
    //获取分页数量
    Long count = brandService.getBrandCount();
    List<DtsBrand> dtsBrands = brandService.getBrandList(page, limit, sort, order,id,name);
    brandListVO.setTotal(count);
    brandListVO.setItems(dtsBrands);
    return ResponseUtil.ok(brandListVO);
  }

  @GetMapping("/catAndAdmin")
  public Object catAndAdmin() {
    CatAndAdminVO catAndAdminVO = new CatAndAdminVO();
    List<DtsAdmin> adminList = adminService.getAdminList();
    List<DtsCategory> categoryList = categoryService.getCategoryList();

    //格式化管理员信息
    List<CatVo> collect = adminList.stream().map(item -> {
      CatVo catVo = new CatVo();
      catVo.setValue(item.getId());
      catVo.setLabel(item.getUsername());
      return catVo;
    }).collect(Collectors.toList());

    //格式化分类信息
    List<CatVo> categoryCatVos = new ArrayList<>();
    for (int i = 0; i < categoryList.size(); i++) {
      CatVo parentCatVo = new CatVo();
      parentCatVo.setValue(categoryList.get(i).getId());
      parentCatVo.setLabel(categoryList.get(i).getName());
      ArrayList<CatVo> catVos = new ArrayList<>();
      for (DtsCategory dtsCategory : categoryList) {
        Integer id = dtsCategory.getId();
        String name = dtsCategory.getName();
        //如果第一层的id和第二层的pid相等,就说明是子类
        if (Objects.equals(categoryList.get(i).getId(), dtsCategory.getPid())) {
          CatVo catVo = new CatVo();
          catVo.setValue(id);
          catVo.setLabel(name);
          //放入集合
          catVos.add(catVo);
        }
      }
      //不管catVos里有没有值都是parentCatVo的child
      parentCatVo.setChildren(catVos);
      // TODO 这里有问题 有没有更灵活的办法
      if (categoryList.get(i).getPid() == 0) {
        categoryCatVos.add(parentCatVo);
      }
    }


    catAndAdminVO.setAdminList(collect);
    catAndAdminVO.setCategoryList(categoryCatVos);
    return ResponseUtil.ok(catAndAdminVO);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "添加")
  @RequiresPermissions(value = {"admin:brand:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsBrand dtsBrand) {
    DtsBrand dtsBrandRes = brandService.createBrand(dtsBrand);
    return ResponseUtil.ok(dtsBrandRes);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "编辑")
  @RequiresPermissions(value = {"admin:brand:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsBrand dtsBrand) {
    DtsBrand dtsBrandRes = brandService.updateBrand(dtsBrand);
    return ResponseUtil.ok(dtsBrandRes);
  }

  @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "删除")
  @RequiresPermissions(value = {"admin:brand:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsBrand dtsBrand) {
    Boolean b = brandService.deleteBrand(dtsBrand);
    if (b) {
      return ResponseUtil.ok();
    }
    return ResponseUtil.fail();
  }
}
